/*
 * Project it.battlehorse.rcp.tools
 * Created on Nov 18, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.actors;

import it.battlehorse.rcp.tools.commons.EventType;
import it.battlehorse.rcp.tools.commons.Severity;
import it.battlehorse.rcp.tools.commons.actors.IActorExecutionStatus;
import it.battlehorse.rcp.tools.commons.actors.IValidationStatus;
import it.battlehorse.rcp.tools.log.ILoggable;

import java.util.LinkedList;
import java.util.List;

/**
 * Defines an event which may be raised by the actor runner
 * 
 * @author battlehorse
 * @since Nov 18, 2005
 */
public class ActorRunnerEvent implements ILoggable {
	
	/**
	 * The name of the actor which raised the event
	 */
	public String actorName;
	
	/**
	 * The id of the actor which raised the event
	 */
	public String actorId;
	
	/**
	 * The timestamp of the event
	 */
	public long when;
	
	/**
	 * The event type
	 */
	public EventType type;
	
	/**
	 * The validation status associated with the event (if the type is {@code VALIDATION_EVENT})
	 */
	public IValidationStatus validationStatus;
	
	/**
	 * The execution status associated with the event (if the type is {@code EXECUTION_STATUS_EVENT})
	 */
	public IActorExecutionStatus<?> executionStatus;
	
	/**
	 * The exception associated with the event (if the type is {@code EXCEPTION_EVENT}
	 */
	public Exception executionException;
	
	/**
	 * Creates a new instance of the class, preconfigured for handling validation statuses
	 * 
	 * @param actorId The id of the actor which raised the event
	 * @param actorName The name of the actor which raised the event
	 * @param validationStatus The validation status
	 */
	public ActorRunnerEvent(String actorId, String actorName, IValidationStatus validationStatus) {
		this.type = EventType.VALIDATION_EVENT;
		this.validationStatus = validationStatus;
		this.when =  System.currentTimeMillis();
		this.actorName = actorName;
		this.actorId = actorId;
	
	}
	
	/**
	 * Creates a new instance of the class, preconfigured for handling execution statuses
	 * 
	 * @param actorId The id of the actor which raised the event
	 * @param actorName The name of the actor which raised the event
	 * @param executionStatus The execution status
	 */
	public ActorRunnerEvent(String actorId, String actorName, IActorExecutionStatus<?> executionStatus) {
		this.type = EventType.EXECUTION_STATUS_EVENT;
		this.executionStatus = executionStatus;
		this.when =  System.currentTimeMillis();
		this.actorName = actorName;
		this.actorId = actorId;
	}
	
	/**
	 * Creates a new instance of the class, preconfigured for handling exceptions
	 * 
	 * @param actorId The id of the actor which raised the event
	 * @param actorName The name of the actor which raised the event
	 * @param executionException the execution exception
	 */
	public ActorRunnerEvent(String actorId, String actorName, Exception executionException) {
		this.type = EventType.EXCEPTION_EVENT;
		this.executionException = executionException;
		this.when =  System.currentTimeMillis();
		this.actorName = actorName;
		this.actorId = actorId;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getMessage()
	 */
	public String getMessage() {
		String message = null;
		if (type == EventType.VALIDATION_EVENT) {
			
			message = validationStatus.getValidationMessage();
		}			
		else if (type == EventType.EXCEPTION_EVENT) {
			
			message = executionException.getMessage() == null ? 
						executionException.getClass().getName() :
						executionException.getMessage();
		}
		else if (type == EventType.EXECUTION_STATUS_EVENT) {
			
			IActorExecutionStatus<?> status = executionStatus;
			message = status.getMessage();
		}
		
		return message;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getParentLog()
	 */
	public ILoggable getParentLog() {
		return null;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSeverity()
	 */
	public Severity getSeverity() {
		
		Severity severity = null;
		if (type == EventType.VALIDATION_EVENT) {
			
			severity = validationStatus.getSeverity();
		}			
		else if (type == EventType.EXCEPTION_EVENT) {
			
			severity = Severity.ERROR;
		}
		else if (type == EventType.EXECUTION_STATUS_EVENT) {
			
			IActorExecutionStatus<?> status = executionStatus;
			severity = status.getSeverity();
		}
		
		return severity;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSource()
	 */
	public Object getSource() {
		return actorName;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getSubLogs()
	 */
	public List<ILoggable> getSubLogs() {
		List<ILoggable> subLogs = null;
		if (type == EventType.VALIDATION_EVENT && 
			    validationStatus.getSubStatuses() != null &&
			    validationStatus.getSubStatuses().size() > 0) {
			
			subLogs = new LinkedList<ILoggable>();
				
			for (IValidationStatus v : validationStatus.getSubStatuses()) {
				ActorRunnerEvent e = new ActorRunnerEvent(actorId,actorName,v);
				e.when = when;
				subLogs.add(e);
			}
		}
		
		return subLogs;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getThrowable()
	 */
	public Throwable getThrowable() {
		return executionException;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getTime()
	 */
	public long getTime() {
		return when;
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.log.ILoggable#getType()
	 */
	public EventType getType() {
		return type;
	}
	
	
	
	
	
}
